package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.qiniu.storage.model.FileListing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.OrderSettingService;
import service.SetmealService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = OrderSettingService.class )
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
   private OrderSettingDao orderSettingDao;

    /**
     * 预约设置服务,批量添加
     */
    @Override
    public void addOrderSetting(ArrayList<OrderSetting> orderSettingList) {
        if (orderSettingList!=null&&orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                //根据日期查询在t_ordersetting 的数量

                int count=orderSettingDao.findCountByDate(orderSetting.getOrderDate());
                if (count>0){
                    //存在.更新
                    orderSettingDao.updata(orderSetting);

                }else{
                    //不存在 插入
                    orderSettingDao.addOrderSetting(orderSetting);
                }


            }

        }
    }

    //查询展示预约设置信息
    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String data) { //2019-3
        String startdata=data+"-1";//2019-3-1
        String enddata=data+"-31";//2019-3-31
        HashMap<String,String> hm = new HashMap();
        hm.put("startdata",startdata );
        hm.put("enddata",enddata );

        ArrayList<Map<String, Object>> list = new ArrayList();
        List<OrderSetting> orderSettingList=orderSettingDao.getOrderSettingByMonth(hm);



        for (OrderSetting orderSetting : orderSettingList) {
            HashMap<String,Object> hm2 = new HashMap();
            hm2.put("date", orderSetting.getOrderDate().getDate());//获得日期（几号）
            hm2.put("number", orderSetting.getNumber());//可预约人数
            hm2.put("reservations", orderSetting.getReservations());//已预约人数
            list.add(hm2);
        }

        return list;
    }

//    基于日历实现预约设置,设置对应日期的可预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //根据日期查询在t_ordersetting 的数量

        int count=orderSettingDao.findCountByDate(orderSetting.getOrderDate());
        if (count>0){
            //存在.更新
            orderSettingDao.updata(orderSetting);

        }else{
            //不存在 插入
            orderSettingDao.addOrderSetting(orderSetting);
        }

    }
}


